/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.transporte.dao.imp;

import ar.com.transporte.config.HibernateUtil;
import ar.com.transporte.dao.RolDao;
import ar.com.transporte.dominio.Rol;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;


/**
 *
 * @author Daniela
 */
public class RolDaoImp implements RolDao {

    public List<Rol> getAll() {
        List<Rol> lista = null;
        try{
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Rol.class);
            lista = criteria.list();
           //session.close();
        }catch(Exception e){}
        return lista;
    }

    public List<Rol> getAll(int maxResults, int firstResult) {
        List<Rol> lista = null;
        try{
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Rol.class);
            criteria.setFirstResult(firstResult);
            criteria.setMaxResults(maxResults);
            lista = criteria.list();
           //session.close();
        }catch(Exception e){}
        return lista;
    }
    public Rol getItem(int id) {
        Rol rol = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Rol.class);
            criteria.add(Restrictions.eq("id", id));
            rol = (Rol)criteria.list().get(0);
        } catch (Exception e) {}
        return rol;
    }

    public void insert(Rol rol) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(rol);
            session.getTransaction().commit();
            //session.close();
        } catch (Exception e) {}
    }

    public void update(Rol rol) {
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Rol.class);
            criteria.add(Restrictions.eq("id", rol.getId()));            
            session.update(rol);
            session.getTransaction().commit();
            //session.close();
        } catch (Exception e) {}
    }

    public void delete(Rol rol) {
        Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Rol.class);
            criteria.add(Restrictions.eq("id", rol.getId()));            
            session.delete(rol);
            session.getTransaction().commit();
    }

    public int getRolCount() {
        int numeroRegistros = 0;
        try{
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            Criteria criteria = session.createCriteria(Rol.class);
            criteria.setProjection(Projections.rowCount());
            numeroRegistros = (Integer)criteria.list().get(0);
           //session.close();
        }catch(Exception e){}
        return numeroRegistros;
    }
}
